package com.woniu.new_orange.service_model_gao.controller;

import com.woniu.new_orange.entity.PassengerInformation;
import com.woniu.new_orange.service_model_gao.dto.PassengerDto;
import com.woniu.new_orange.service_model_gao.service.PassengerService;
import com.woniu.new_orange.service_model_gao.vo.PassengerVo;
import com.woniu.new_orange.util.ObjectExcelView;
import com.woniu.new_orange.util.PageData;
import com.woniu.new_orange.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Program: new_orange->PassengerController
 * @Description: 乘客信息表controller
 * @Author: gao chaozhe
 * @Date: 2023-02-06 14:09
 * @Version： 1.0
 **/
@RestController
@RequestMapping("passengers")
@Api(value = "旅客管理",tags="旅客管理")
public class PassengerController {
    @Autowired
    private PassengerService passengerService;

    @PostMapping("/getAll")
    @ApiOperation(value = "获取当前用户id下旅客表格全部数据",notes = "获取当前用户id下旅客表全部数据返回给前端页面展示")
    public Result getAll(@RequestBody PassengerDto passengerDto){
        List<PassengerVo> passengerVoList = passengerService.selectAll(passengerDto);
        return Result.ok().data("passengerList",passengerVoList);
    }
    @PostMapping("")
    @ApiOperation(value = "向旅客表中添加数据",notes = "从前端页面获取数据后存入数据库中")
    public Result add(@RequestBody PassengerInformation record) {
        int i = passengerService.insertSelective(record);
        return Result.ok();
    }

    @RequestMapping("/getOne/{id}")
    @ApiOperation(value = "前端选取某条数据id",notes = "通过获取的id从数据库提取该条数据展示")
    public Result getOne(@PathVariable long id){
        PassengerInformation passengerInformation = passengerService.selectByPrimaryKey(id);
        return Result.ok().data("passengerList",passengerInformation);
    }
    @PutMapping("")
    @ApiOperation(value = "对获取的单条数据某些数据进行更改",notes = "更改完毕存入数据库")
    public Result update(@RequestBody PassengerInformation record){
        int i = passengerService.updateByPrimaryKeySelective(record);
        return Result.ok();
    }

    @DeleteMapping("{id}")
    @ApiOperation(value = "前端选取某条数据id",notes = "通过获取的id从数据库删除该条数据")
    public  Result del(@PathVariable long id){
        int i = passengerService.deleteByPrimaryKey(id);
        return Result.ok();
    }

    @RequestMapping(value = "/exportExcel")
    @ApiOperation(value = "导出excel表",notes ="可以根据所需内容导出" )
    public ModelAndView exportExcel(@RequestBody PassengerDto passengerDto) {
        ModelAndView mv = new ModelAndView();
        try {
            Map<String, Object> dataMap = new HashMap<>();
            //构造列名称
            List<String> titles = new ArrayList<>();
            titles.add("旅客姓名"); // 1
            titles.add("旅客身份证号码"); // 2
            titles.add("旅客手机号码"); // 3
            titles.add("旅客年龄"); // 4
            titles.add("旅客性别"); // 5
            dataMap.put("titles", titles);
            //得到被导出的数据
            List<PassengerVo> passengerVoList = passengerService.selectAll(passengerDto);
            List<PageData> varList = new ArrayList<>();
            for (PassengerVo passengerVo : passengerVoList) {
                PageData vpd = new PageData();
                vpd.put("var1", passengerVo.getPassengerName1());
                vpd.put("var2", passengerVo.getPassengerIdCard1());
                vpd.put("var3", passengerVo.getPassengerPhone1());
                vpd.put("var4", passengerVo.getPassengerAge1());
                vpd.put("var5", passengerVo.getPassengerSex1());
                varList.add(vpd);
            }
            dataMap.put("varList", varList);
            ObjectExcelView erv = new ObjectExcelView();
            mv = new ModelAndView(erv, dataMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mv;
    }

}


